//
//  ClientEngine.m
//  viewtest
//
//  Created by xy on 10-2-9.
//  Copyright 2010 HANDPAY. All rights reserved.
//
#import "ZZTongClientEngine.h"
#import "HPClientEngine.h"
#import "Utility.h"
#import "SecureManager.h"
#include "luaEngine.h"
#import "HPUIEngine.h"
#import "DefineUtil.h"

static HPClientEngine * ce=nil;
//static const BOOL COMPILED=NO;

@implementation SaveData
@synthesize data;
-(id)initwithData:(HPData *)datai{
	if (self = [super init]) {
		self.data=datai;
    }
    return self;
}

- (void)dealloc{
    if (self.data) {
        releaseHPData(self.data);
        self.data = NULL;
    }
    [super dealloc];
}
 
@end


@implementation HPClientEngine

@synthesize se;
@synthesize rms;
@synthesize hashGlobalData;
@synthesize securmanager;
@synthesize netStatus;
@synthesize globalData;
@synthesize hpAppDelegate;
@synthesize appServer;

-(void)initengineUI{
    SecureManager* tempSec = [[SecureManager alloc] init];
	self.securmanager=tempSec;
	[tempSec release];
    
    StreamEngine* tempSe = [[StreamEngine alloc] init];
	self.se=tempSe;
	[tempSe release];
    
	self.rms=[NSUserDefaults standardUserDefaults];
	self.hashGlobalData=[NSMutableDictionary dictionaryWithCapacity:10];
	self.netStatus = 0;
    self.globalData=CreateTABLEHPData();
}


- (void)dealloc{
    self.se = nil;
    self.rms = nil;
    self.hashGlobalData = nil;
    self.securmanager = nil;
    self.hpAppDelegate = nil;
    self.appServer = nil;
    if (self.globalData) {
        releaseHPData(self.globalData);
        self.globalData = NULL;
    }
    [super dealloc];
}

+(void)releaseInstance{
    if (ce) {
        [ce release];
        ce = nil;
    }
}

+(HPClientEngine *)getHPClientEngine{
	if (ce==nil) {
		ce=[[HPClientEngine alloc]init];
		[ce initengineUI];
	}
	return ce;
}

-(void)startApp{
#if defined (ZZTSetUrl) && (ZZTSetUrl == 1)
    [ZZT_CLIENG.uiEngine showSetUrlScreen];
#else
    [HPUIENG showWelcomeScreen];
    
    if (self.hpAppDelegate) {
        [self.hpAppDelegate initApp];
    }
#endif
    
}

-(BOOL)isRMSHaveDataWithKey:(NSString*)key{
    if (self.rms == nil || key == nil) {
        return NO;
    }
    NSString* data = (NSString*)[self.rms dataForKey:key];
    if (data) {
        return YES;
    }
    return NO;
}

/**
 * 永久保存数据
 * 参数key：保存关键字
 *    data：保存数据
 */
-(void)SaveRMSData:(NSString *) key withStr:(NSString *)str{
    if (self.rms==nil || key==nil) {
		return;
	}
    
	[self.rms removeObjectForKey:key];
    
    if (!str) {
        return;
    }
    
    NSString * rmsstr = [NSString stringWithFormat:@"s%@",str];
    [self.rms setObject:rmsstr forKey:key];
    [self.rms synchronize];    
}

/**
 * 永久保存数据
 * 参数key：保存关键字
 *    data：保存数据 
 */
-(void)SaveRMSData:(NSString *) key withData:(const HPData *) data{
	if (self.rms==nil || key==nil) {
		return;
	}
	[self.rms removeObjectForKey:key];
    
	if (data!=NULL) {
		NSString * rmsstr=nil;
		char * tempss=NULL;
		
		switch (data->type) {
			case LUA_TNUMBER:
				rmsstr=[NSString stringWithFormat:@"d%f.2",data->datad];
				break;
			case LUA_TSTRING:
				if (data->datas!=NULL) {
					rmsstr=[NSString stringWithUTF8String:data->datas];
					rmsstr=[NSString stringWithFormat:@"s%@",rmsstr];
				}
				break;
			case LUA_TBOOLEAN:
				rmsstr=[NSString stringWithFormat:@"b%d",data->datab];
				break;
			case LUA_TTABLE:
				tempss=HPDataToString(data,"rmsdata");
				if (tempss!=NULL) {
					rmsstr=[NSString stringWithUTF8String:tempss];
					rmsstr=[NSString stringWithFormat:@"t%@",rmsstr];
					free(tempss);
				}
				break;
		}
		if (rmsstr!=nil) {
			[self.rms setObject:rmsstr forKey:key];
			//[self.rms setObject:rmsd forKey:key];
		}
	}
	[self.rms synchronize];
}

/**
 * 读取永久保存数据
 * 参数key：保存关键字
 */
-(NSString *)GetRMSString:(NSString *) key{
	if (!self.rms || !key) {
		return nil;
	}
    
	NSString * rmsd=[self.rms stringForKey:key];
    if (!rmsd) {
        return nil;
    }
    
    if (![rmsd hasPrefix:@"s"]) {
        return nil;
    }
    
    return [rmsd substringFromIndex:1];
}

/**
 * 读取永久保存数据
 * 参数key：保存关键字
 */
-(HPData *)GetRMSData:(NSString *) key{
	HPData * rdata=NULL;
	if (self.rms==nil || key==nil) {
		return rdata;
	}
    
	NSString * rmsd=[self.rms stringForKey:key];
	if (rmsd!=nil) {
		unichar indchr=[rmsd characterAtIndex:0];
		NSString * data=[rmsd substringFromIndex:1];
		if (indchr=='d') {
			double dd=[data doubleValue];
			rdata=CreateNUMHPData(dd);
		}
		else if(indchr=='s'){
			const char * str=[data UTF8String];
			rdata=CreateSHPData(str);
		}
		else if(indchr=='b'){
			int bb=[data intValue];
			rdata=CreateBOOLHPData(bb);
		}
		else if(indchr=='t'){
			const char * temss=[data UTF8String];
			rdata=CreateHPDataFromString(temss,"rmsdata",(int)(strlen(temss)));
		}
	}
	return rdata;
}

/**
 * 临时保存数据
 * 参数key：保存关键字
 *    data：保存数据
 */
-(void)SaveGlobalData:(NSString *) data withKey:(NSString *)key{
	NSString * old=[self.hashGlobalData objectForKey:key];
	if (old) {
		[self.hashGlobalData removeObjectForKey:key];
        
	}
	
    if (!data) {
        return;
    }
   
    [self.hashGlobalData setObject:data forKey:key];    
}


/**
 * 读取临时保存数据
 * 参数key：保存关键字
 */
-(NSString *)GetGlobalData:(NSString *)key{
	NSString * sd=[self.hashGlobalData objectForKey:key];
	return  sd;
}



//获取软件版本
-(NSString*)getHeadVersion{
    if (self.hpAppDelegate) {
        return [self.hpAppDelegate headServerVersion];
    }
    return nil;
}


-(NSString*)getUrlByName:(NSString*)name{
    if (self.hpAppDelegate) {
        return [self.hpAppDelegate getUrlByName:name];
    }
    return nil;
}

@end
